Skip to content

fix(cas): create the conversation and start its session on chat init [JAR-10400] - #215

Merged
andrewwan-uipath merged 2 commits into
developfrom
fix/cas-conversation-lifecycle-jar-10400
Nov 24, 2026
Merged

andrewwan-uipath merged 2 commits into
developfrom
fix/cas-conversation-lifecycle-jar-10400

Conversation

@andrewwan-uipath

@andrewwan-uipath andrewwan-uipath commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes chat Flows getting stuck at Wait-for-Message (JAR-10400), found on alpha.

The widget created the conversation on the first send and started its session at the same time, so the create, the session start and the first exchange all reached CAS together. That can lose the first message on the Flow side, leaving the Flow waiting forever. The session also never started until the first send, unlike the react-sdk, which starts it as soon as a conversation is selected (Temporal agents rely on this).

This now matches the react-sdk: the conversation is created on load and on New Chat, and its session starts right away.

Changes

ConversationalAgentChat.tsx:

  • Session: one effect owns the active conversation's session. It starts as soon as a conversation is active (load, New Chat, inputs form, existing conversation, opening one from history) and ends on switch or unmount, so a switch always ends the old session before starting the new one. Ended and read-only conversations get no session.
  • Create: startConversation() is the only create path. Load and New Chat create up front while the chat is already showing. A message, attachment or settings-panel input sent during the create waits for it and uses the same conversation. If the create fails or takes longer than 30 seconds, a banner is shown and the next send retries.
  • Moving on mid-create: if the user starts another New Chat or opens a history conversation, or the host switches agent, user or conversation, the pending create is ignored and anything waiting on it is dropped without an error.
  • Also fixed: applying optional inputs from the settings panel before the first message no longer fails, and a conversation created while the first history page loads stays in the sidebar.
  • Unchanged: agents with required inputs still create the conversation from the inputs page on submit (now with its session started), and debug mode still creates on the first send.

agents.md is updated to describe the new lifecycle.

Testing

  • New tests cover each load/New Chat/switch case: create and session start on load, StrictMode, send and attachments during the create, settings inputs before the first message, create failure and timeout, existing and ended conversations, read-only, debug mode, the inputs page, New Chat, switching (including reopening the same conversation), moving on mid-create, host switches and unmount. Each fails on develop or when its safeguard is removed.
  • vitest run in conversational-agent-chat: 451 pass. tsc, ESLint, Prettier and the package build are clean.
  • Manually in Storybook on alpha: POST /conversation and startSession go out on load before any input, and the first send reuses that session.

Demos

Screen.Recording.2026-11-23.at.10.32.01.AM.mov
Screen.Recording.2026-11-23.at.10.33.41.AM.mov
Screen.Recording.2026-11-23.at.10.35.58.AM.mov
Screen.Recording.2026-11-24.at.10.54.38.AM.mov
Screen.Recording.2026-11-24.at.11.28.54.AM.mov

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Toggling readOnly leaves sessionReady resolved to the ended session, causing subsequent sends to target it instead of the newly started session.

1 open issue
What changed in this PR

This PR addresses JAR-10400 by creating conversations and starting sessions during chat initialization, rather than waiting for the first message, to prevent Flows from getting stuck at Wait-for-Message.

Changes:

  • Centralizes conversation creation and session cleanup across load, New Chat, and conversation switches.
  • Shares pending creates across sends, attachments, and settings updates, with timeout and stale-result handling.
  • Adds lifecycle regression tests and updates the lifecycle documentation.
File Description
packages/​conversational-agent-chat/​src/​ConversationalAgentChat.tsx Implements upfront creation, effect-owned sessions, and pending-operation handling.
packages/​conversational-agent-chat/​src/​__tests__/​ConversationalAgentChat.test.tsx Adds coverage for initialization, retries, switching, and unmount behavior.
packages/​conversational-agent-chat/​agents.md Documents the revised conversation and session lifecycle.

Review effort: Balanced

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

📊 Coverage Report

Package Line Coverage
datatable 88.29%
multi-file-upload 87.27%
conversational-agent-chat 91.63%
validation-station 99.33%
classification-station 91.66%
document-viewer 89.38%
external-auth 97.26%
pdf-viewer 96.26%
decisions-and-reasons 98.50%

@andrewwan-uipath
andrewwan-uipath force-pushed the fix/cas-conversation-lifecycle-jar-10400 branch from e27a890 to f5d5578 Compare November 20, 2026 00:42
@andrewwan-uipath
andrewwan-uipath requested a balanced review from Copilot November 20, 2026 00:46
@andrewwan-uipath
andrewwan-uipath force-pushed the fix/cas-conversation-lifecycle-jar-10400 branch from f5d5578 to a9188d0 Compare November 20, 2026 00:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The readOnly effect checks stale conversation state, allowing an already-ended conversation to start another session when readOnly is toggled on and off.

1 open issue
1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A pending history request can overwrite an updated conversation label with the original create-response label because the merge uses a stale active-conversation reference.

1 open issue
1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

An in-flight send can use a replacement conversation’s session or an ended session because cancellation is not checked across awaits, risking delivery to the wrong conversation.

1 open issue
1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A delayed history response can drop a locally created conversation with messages from the sidebar because only the currently active conversation is preserved.

1 open issue
1 resolved since last review

Review effort: Balanced

…[JAR-10400]

The widget created the conversation on the first send and started the
session with it, so the create, session start and first exchange all
reached CAS together, which can leave a chat Flow stuck at
Wait-for-Message. The session also never started before the first send,
unlike the react-sdk, which Temporal agents rely on.

- One session effect owns the active conversation's session: it starts as
  soon as a conversation is active (load, New Chat, inputs form, existing
  conversation, history switch) and ends on switch or unmount. Ended and
  read-only conversations get no session.
- startConversation() is the only create path. Load and New Chat create up
  front behind the mounted chat; sends, attachments and the settings panel
  join the in-flight create. A failed or timed-out (30s) create shows a
  banner and the next send retries.
- Work waiting on a conversation the user or host moved away from is
  dropped silently; a superseded init run stops at its next await.
- The settings panel can apply optional inputs before the first message,
  and a conversation created while the first history page loads stays in
  the sidebar.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the conversation creation and session lifecycle changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@@ -11,6 +11,7 @@ import { getI18n } from "../i18n";
import { ConversationalAgentChat } from "../ConversationalAgentChat";
import { UiPath } from "@uipath/uipath-typescript/core";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the first demo video in your PR - the "Generating..." disappears fast for that Flow chat agent (it seems like its disappearing right at the end of the agent response). Just checking that it doesn't stop showing the "Generating..." until the end-exchange event?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the widget only stops showing the generating onExchangeEnd or when the user hits stop

Comment on lines +154 to +155
const hasRequiredInputs = (schema: InputSchema | null | undefined) =>
(schema?.required?.length ?? 0) > 0;

@maxduu maxduu Nov 24, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh thanks - so there was a bug before with optional inputs and it works now (directly initializes the chat just like no-inputs, when inputs are all optional)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional-only inputs already skipped the form on develop I believe. The only change is that the conversation is now created on load instead of on the first send, same as agents with no inputs.

Comment on lines +903 to +905
// The only way a conversation is created. Everything that needs one (load,
// New Chat, the inputs form, a send, an attachment, the settings panel)
// joins the one in-flight create rather than starting a second.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rewrite the comments across all the changes to be a bit more explanatory / human-readable? I think Claude is kinda verbose sometimes / unnecessary in its details :)

Comment on lines +974 to +975
// For the creates nothing awaits (load, New Chat): a failure becomes a
// banner, and the next send retries.

@maxduu maxduu Nov 24, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, what does the screen look like when a load/new-chat fails? Could you add a screenshot of that to the PR description

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a short video to the PR description

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good thanks!

// Load and New Chat create the conversation up front under the same rule.
const shouldCreateUpfront = useCallback(
(agent: AgentGetByIdResponse | null | undefined, requiredInputs: boolean) =>
!!agent && !requiredInputs && !readOnly && !isDebugMode,

@maxduu maxduu Nov 24, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - i see the !isDebugMode. Just verifying that you tested debug mode as well end-to-end, where you pass in the debug chat id?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the unit tests cover it, but will test it in flow and add a video to the PR description

// The only way a conversation is created. Everything that needs one (load,
// New Chat, the inputs form, a send, an attachment, the settings panel)
// joins the one in-flight create rather than starting a second.
const startConversation = useCallback(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if important but my coding agent suggested:

startConversation(agentInput) silently drops agentInput when a create is already pending. if (pendingCreate.current) return pendingCreate.current; returns the in-flight create regardless of arguments, so an inputs-form submit would adopt an input-less conversation. Unreachable today only because shouldCreateUpfront returns false when the agent has required inputs — an invariant held at a distance with nothing local enforcing it. A future change (pre-creating for required-input agents, say) would produce conversations missing their required inputs, failing agent-side rather than at compile time. Cheapest fix: skip the dedupe when agentInput is passed, or assert.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, should be fixed now

Comment on lines +215 to +230
const [activation, setActivation] = useState<{
conversation: ConversationCreateResponse | null;
}>({ conversation: null });
const activeConversationRef = useRef<ConversationCreateResponse | null>(null);
// Bumped whenever the active conversation changes, so a create started for
// an earlier one never becomes active.
const conversationGeneration = useRef(0);
// The in-flight create, shared by everything waiting on a conversation.
const pendingCreate = useRef<Promise<ConversationCreateResponse> | null>(
null,
);
// Conversations this widget created, so the first history page (which can
// land after the create) doesn't drop the active one from the sidebar.
const createdConversationIds = useRef(new Set<string>());
// Bumped per init run (and on unmount) so a superseded run stops at its next await.
const initRun = useRef(0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify all these at all? Claude suggested:

conversationGeneration is redundant. It's checked in exactly two places (lines 939, 953), and the .finally three lines below already demonstrates the equivalent test:

if (pendingCreate.current === pending) pendingCreate.current = null;
Every path that bumps the generation also nulls pendingCreate — activateConversation (824–825) and the unmount cleanup (2119–2120), and there is no third. So pendingCreate.current !== pending detects supersession in exactly the same cases. Arguably it's more correct: a future "cancel this create" path would be caught by promise identity and missed by a generation that only tracks conversation changes. The counter reads more intentionally, so it's a judgment call rather than a defect — but it's one less invariant to keep in sync.

createdConversationIds can be a boolean. It's consulted once, and only ever as has(active.id) — the merge concerns the currently-active conversation and nothing else. One flag set in activateConversation does the same job without a Set that grows for the session's life. Slight nuance: with a Set, leaving a created conversation and reopening it from history keeps it pinnable; with a flag it wouldn't. That difference is close to immaterial, since by then it's in the server page anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both done, thanks

- Detect a superseded create by checking it's still the pending one,
  instead of a separate generation counter.
- Track whether the active conversation was created here with a flag
  instead of a Set of ids.
- If inputs are passed while a create without them is in flight, attach
  them with updateById rather than dropping them.
- Rewrite the lifecycle comments to be shorter and clearer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified in the conversation-creation and session-lifecycle changes.

No open issues 🎉

Review effort: Balanced

@andrewwan-uipath
andrewwan-uipath merged commit c052b0f into develop Nov 24, 2026
6 checks passed
@andrewwan-uipath
andrewwan-uipath deleted the fix/cas-conversation-lifecycle-jar-10400 branch November 24, 2026 20:33
andrewwan-uipath added a commit that referenced this pull request Nov 24, 2026
Release the conversation lifecycle fix (#215), the locale label fix
(#214), the first-run title fix (#207) and the opt-in Wind renderer
(#201). 1.2.0 sorts after the 1.2.0-beta.1 the iframe chat pins today.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants